#!/usr/bin/env python3
"""
修复导入路径脚本
将所有 biomni_web. 前缀的导入改为相对导入
"""

import os
import re

def fix_imports_in_file(file_path):
    """修复单个文件中的导入"""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        # 替换 from biomni_web.xxx import 为 from xxx import
        original_content = content
        content = re.sub(r'from biomni_web\.([^\s]+) import', r'from \1 import', content)
        
        # 如果内容有变化，写回文件
        if content != original_content:
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(content)
            print(f"已修复: {file_path}")
            return True
        return False
    except Exception as e:
        print(f"修复文件 {file_path} 时出错: {e}")
        return False

def main():
    """主函数"""
    # 需要修复的文件列表
    files_to_fix = [
        'routes/api_routes.py',
        'routes/user_routes.py',
        'routes/agent_routes.py',
        'utils/response_utils.py',
        'utils/token_utils.py',
        'utils/session_utils.py',
        'services/query_service.py',
        'services/agent_service.py',
        'services/cost_service.py',
        'services/user_service.py',
        'start_web.py'
    ]
    
    fixed_count = 0
    for file_path in files_to_fix:
        if os.path.exists(file_path):
            if fix_imports_in_file(file_path):
                fixed_count += 1
        else:
            print(f"文件不存在: {file_path}")
    
    print(f"\n总共修复了 {fixed_count} 个文件")

if __name__ == '__main__':
    main()